package com.jiangjiang.eshop.comment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jiangjiang.eshop.comment.constant.CommentPictureUploadDirType;
import com.jiangjiang.eshop.comment.domain.CommentPictureEntity;
import com.jiangjiang.eshop.comment.mapper.CommentPictureMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.Wrapper;
import java.util.*;

/**
 * @ClassName CommentPictuireService
 * @Description TODO
 * @Author jhw
 * @Date 2021/5/31 19:57
 * @Version 1.0
 **/
@Slf4j
@Service
public class CommentPictureService extends ServiceImpl<CommentPictureMapper, CommentPictureEntity> {

    @Autowired
    private CommentPictureMapper commentPictureMapper;

    /**
     * 评论图片项目上传目录类型：relative 表示相对路径  absolute 表示绝对路径
     */
    @Value("${comment.picture.upload.dir.type}")
    private String uploadDirType;

    /**
     * 评论图片上传的目录
     */
    @Value("${comment.picture.upload.dir}")
    private String uploadDirPath;


    /**
     * 保存评论图片
     */
    public void saveCommentPicture(String appBasePath, Long commentInfoId, MultipartFile[] files) throws IOException {

        String realUploadPath = uploadDirPath;

        if (CommentPictureUploadDirType.RELATIVE.equals(uploadDirType)) {
            realUploadPath = appBasePath + uploadDirPath;
        }

        // 将图片上传到指定目录
        File uploadDir = new File(realUploadPath);
        if (!uploadDir.exists()) {
             uploadDir.mkdir();
        }

        List<CommentPictureEntity> entries = Lists.newArrayList();
        for (MultipartFile file : files) {
            if (Objects.isNull(file)) {
                continue;
            }
            // 如果目标文件已经存在，则删除目标文件
            String fileSeparator = System.getProperties().getProperty("file.separator");
            String targetFilePath = realUploadPath + fileSeparator + file.getOriginalFilename();
            File targetFile = new File(targetFilePath);
            if (targetFile.exists()) {
                targetFile.delete();
            }

            // 讲上传上来的文件保存到指定的文件目录下
            file.transferTo(targetFile);

            // 创建一个评论图片对象
            CommentPictureEntity commentPictureEntity = new CommentPictureEntity();
            commentPictureEntity.setCommentInfoId(commentInfoId);
            commentPictureEntity.setCommentPicturePath(targetFilePath);
            commentPictureEntity.setGmtCreate(new Date());
            commentPictureEntity.setGmtModified(new Date());
            entries.add(commentPictureEntity);
        }

        if(CollectionUtils.isNotEmpty(entries)){
            // 批量保存
            this.saveBatch(entries);
        }
    }

    /**
     * 根据评论id集合查询评论图片
     * @param commentInfoIds
     * @return
     */
    public List<CommentPictureEntity> queryPicturesByCommentInfoIds(Set<Long> commentInfoIds) {
        LambdaQueryWrapper<CommentPictureEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CommentPictureEntity::getCommentInfoId, commentInfoIds);
        return this.getBaseMapper().selectList(wrapper);
    }
}
